<?php
namespace app\admin\controller;
use think\Request;
class Fenlei extends Common
{
    public function index()
    {
        //升序排列
        $fenlei_list = db('goods_fenlei')->order('id asc')->paginate(5);
        $page=$fenlei_list->render();
        //dump($page);die;
        $grade = [
            "1" => '一级菜单',
            "2" => '二级菜单',
        ];
        $page = str_replace(
            ["pagination", 
            "disabled",
            "active"],
        ["am-pagination tpl-pagination",
        "am-disabled",
        "am-active"],
        $page);
        $this->assign('grade' , $grade);
        $this->assign('fenlei_list' , $fenlei_list);
        $this->assign('page' , $page);
        return view();
    }
    //上传分类图
    public function upimg(){
        $file = request()->file('src');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $img_src = '/uploads/'.$info->getSaveName();
                $image = \think\Image::open('.'.$img_src);
                $date =date('Ymd',time());
                $path="uploads/thumbs/$date";
                if(!file_exists($path)){
                    mkdir($path, 0777,true);
                }
                $image->thumb(150,100)->save('.'.'/uploads/thumbs/'.$info->getSaveName());
                $thumb_src='/uploads/thumbs/'.$info->getSaveName();
                return ['src'=>$img_src,'thumb_src'=>$thumb_src];
            }
        }else{
            $this->error($file->getError());
        }
    }
    public function add(){
        $fenlei_list = db('goods_fenlei')->where("grade=1")->select();
        $this->assign('fenlei_list',$fenlei_list);
        return view('add');
    }
    //新增商品分类
    public function insert(){
        $data = input('post.');
        if($data['grade']==1){
            $data['pid']=0;
        }
        $ret = db('goods_fenlei')->insert($data);
        if($ret){
            $this->success('添加成功','admin/fenlei/index');
        }else{
            $this->error('添加失败');
        }
    }
    //轮播图编辑页面
    public function edit()
    {
        $id=input('get.id');
        $result = db('goods_fenlei')->where("id=$id")->find();
        $this->assign('result',$result);
        return view();
    }
    //改轮播图
    public function update()
    {
        $id=input('get.id');
        $data=input('post.');
        $result = db('goods_fenlei')->where("id={$id}")->find();
        $ret=db('goods_fenlei')->where("id=$id")->update($data);
        if($ret == false){
            $this->error('修改失败');
        }else{
            $this->success('修改成功','admin/fenlei/index');
        }
    }

    //删除轮播图
    public function delete(){
        $id=$_GET['id'];
        $ret=db('goods_fenlei')->where("id=$id")->delete();
        if($ret){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败");
        }
    }
}
